/*
 * Window.cpp
 *
 *  Created on: 11.12.2011
 *      Author: serafim
 */

#include <gtkmm.h>
#include <gtkmm/main.h>
#include <gtkmm/builder.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>
#include <gtkmm/progressbar.h>
#include <iostream>

using std::string;
using std::cin;
using std::cout;
using std::endl;

#include <cstdlib>
#include "DataParser.h"
#include "Session.h"
#include "Window.h"
#include "Interface.h"


Window::Window() {

}

Window::~Window() {
}

void Window::init(int argc, char* argv[]){
	Gtk::Main kit(argc, argv);
	Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(UI_FILE);

	builder->get_widget("main_window", _window);
	builder->get_widget("key_entry", _keyword_field);
	builder->get_widget("reznum_sbutton", _num_field);
	builder->get_widget("run_button", _run_button);
	builder->get_widget("button1", _button1);

	builder->get_widget("statusbar", _statusbar);
	builder->get_widget("progressbar", _progress);
	_progress->set_show_text(1);

	_run_button->signal_clicked().connect( sigc::mem_fun(*this, &Window::onRunButtonClicked) );
	_button1->signal_clicked().connect( sigc::mem_fun(*this, &Window::onButton1Clicked) );

	kit.run(*_window);
}

void Window::setProgressPos(double pos){
	_progress->set_fraction(pos);
	update();
}

void Window::update(){
	while (Gtk::Main::events_pending())
		Gtk::Main::iteration();
}

void Window::onRunButtonClicked(){
	string keyword = _keyword_field->get_text();
	int num = _num_field->get_value_as_int();

	if(keyword=="" || num==0)
		return;

	setRunInactive();
	update();

	Session s(keyword, num);
	s.parseTag();
	s.saveResults();

	setProgressText("Завершено");
	setRunActive();
	setProgressPos(0.0);
}

void Window::onButton1Clicked(){
	system("cd scripts && ./Schema.py");
}




